package com.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.sys.SYSConstast.SYSConstast;
import com.sxt.sys.domain.Dept;
import com.sxt.sys.service.DeptService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.ResultObj;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.vo.DeptVo;
import com.sxt.sys.vo.NoticeVo;

@RestController
@RequestMapping("dept")
public class DeptController {
	
	
	@Autowired
	private DeptService deptService;
	
	//查询
	@RequestMapping("loadAllDept")
	public DataGridView loadAllDept(DeptVo deptVo) {
		return deptService.queryAllDept(deptVo);
	}
	
	@RequestMapping("deleteDept")
	public ResultObj deleteDept(DeptVo deptVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = deptService.deleteDept(deptVo.getId());
			code = info;
			msg = info > 0 ?SYSConstast.DELETE_LOGININFOR_TRUE:SYSConstast.DELETE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.DELETE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	@RequestMapping("addDept")
	public ResultObj addNotice(DeptVo DeptVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = deptService.addDept(DeptVo);
			code = info;
			msg = info > 0 ?SYSConstast.ADD_LOGININFOR_TRUE:SYSConstast.ADD_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.ADD_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("updateDept")
	public ResultObj updateNotice(DeptVo deptVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = deptService.updateDept(deptVo);
			code = info;
			msg = info > 0 ?SYSConstast.UPDATE_LOGININFOR_TRUE:SYSConstast.UPDATE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.UPDATE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("loadDeptManagerLeftTreeJson")
	public List<TreeNode> loadDeptManagerLeftTreeJson(DeptVo deptVo){
		List<TreeNode> node = new ArrayList<>();
		List<Dept> list = deptService.queryAllDeptForlist(deptVo);
		for (Dept dept : list) {
			Boolean isParent =dept.getParent()==SYSConstast.DEPT_ISPARENT_TRUE?true:false;
			Boolean open = dept.getOpen()==SYSConstast.DEPT_OPEN_TRUE?true:false;
			node.add(new TreeNode(dept.getId(), dept.getPid(), dept.getName(), isParent, open));
		}
		return node;
		
	}
	
	
	//取出排序最大值
	@RequestMapping("loadMaxOrderNumber")
	public Map<String, Integer> loadMaxOrderNumber(){
		Map<String, Integer> map = new HashMap<>();
		Integer value = deptService.queryMaxOrderNumber();
		
		map.put("value", value);
		
		return map;
	}
	
	@RequestMapping("isThisDeptObjHasChildren")
	public Map<String,Object> isThisDeptObjHasChildren(DeptVo deptVo){
		Map<String,Object> map=new HashMap<>();
		Integer value=this.deptService.isThisDeptObjHasChildren(deptVo.getId());
		map.put("value", value>0?true:false);
		return map;
	}
}
